package com.zhuss.food.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.product.CreateProductBo;
import com.zhuss.bean.vo.food.food.DetailFoodVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.EnumUtil;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.ProductType;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.ProductDao;
import com.zhuss.food.db.entity.Product;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.NoUtil;

/**
 * 创建商品 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/29 16:01
 */
@Component("productCreate")
public class ProductCreate extends AbstractProcessor<Object, CreateProductBo> implements Processor<Object, CreateProductBo> {

    @Autowired
    private ProductDao productDao;
    
    @Autowired
	@Qualifier("productRead")
	private ProductRead productRead;
    
    
    @Autowired
	@Qualifier("foodRead")
	private FoodRead foodRead;
    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateProductBo bo) throws WpsException {
    	Long foodId = bo.getFoodId();
    	Integer area = bo.getArea();
    	Integer type = bo.getType();
    	Date date = bo.getDate();
    	Integer productTime = bo.getProductTime();
    	Integer floor = bo.getFloor();
    	if (foodId == null || foodId < 0l) {
    		throw new WpsException(ExceptionCode.餐品ID为空);
    	}

    	DetailFoodVo food = (DetailFoodVo) foodRead.detail(foodId);
    	if (food == null || food.getFoodId() == null) {
    		throw new WpsException(ExceptionCode.数据不存在);
    	}
    	Integer foodArea = food.getArea();
    	if (!foodArea.equals(area)) {
    		throw new WpsException(ExceptionCode.区域与餐品不匹配);
    	}
    	
    	String name = food.getName();
    	String foodNo = food.getFoodNo();
    	Integer category = food.getCategory();
    	Long price = food.getPrice();
    	bo.setName(name);
    	bo.setFoodNo(foodNo);
    	bo.setCategory(category);
		bo.setPrice(price);
    	
    	if (name == null || "".equals(name.trim())) {
    		throw new WpsException(ExceptionCode.商品name为空);
    	}
    	
    	if (ProductType.一周菜单.sameKey(type)) {
    		List<Product> list = productRead.listWeek(type, foodId, date, productTime, floor, area);
    		if (!list.isEmpty()) {
    			throw new WpsException(ExceptionCode.菜单当天楼层用餐时间已经存在);
    		}
    	}
    	
    	if (ProductType.点炒服务.sameKey(type)) {
    		List<Product> list = productRead.list(type, name, area);
    		if (!list.isEmpty()) {
    			throw new WpsException(ExceptionCode.点炒服务菜品已存在);
    		}
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(CreateProductBo param) throws WpsException {
    	Product record = new Product();
    	record.setProductNo(NoUtil.PRODUCT + NoUtil.gen());
    	record.setCategory(param.getCategory());
    	record.setName(param.getName());
    	record.setType(param.getType());
    	record.setDate(param.getDate());
    	record.setFoodId(param.getFoodId());
    	record.setFoodNo(param.getFoodNo());
    	record.setArea(param.getArea());
    	record.setStartTime(param.getStartTime());
    	record.setEndTime(param.getEndTime());
    	record.setPrice(param.getPrice());
    	record.setTotal(param.getTotal());
    	record.setSort(param.getSort());
    	record.setProductTime(param.getProductTime());
    	record.setFloor(param.getFloor());
    	record.setStatus(param.getStatus());
    	record.setUsed(param.getUsed());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	productDao.create(record);
    	param.setTargetId(record.getProductId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateProductBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.商品创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(EnumUtil.containsKey(ProductType.class, param.getType()) + ":" + param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
